<?php
require_once "./interface/IController.php";

class ActivityController implements IController {
	private $view;
	
	public function __construct() {
		$this->view = new View ();
	}
	
	//设置方法的访问权限，若权限不匹配，则调用  forbidAction()
	public function getAccessRule() {
		return array (UserBean::$_ADMIN => array ("requestacivity", "viewapprovingactivities", "viewapprovedactivities", "viewspecificactivity", "viewallactivities", "approveactivity", "to404" ), UserBean::$_SENATE => array ("requestacivity", "viewapprovingactivities", "viewapprovedactivities", "viewspecificactivity", "viewallactivities", "approveactivity", "to404", "print" ), UserBean::$_INSTITUE => array ("requestacivity", "viewapprovingactivities", "viewapprovedactivities", "viewspecificactivity", "viewallactivities", "approveactivity", "to404", "print" ), UserBean::$_GUEST => array ("to404", "forbid" ) );
	}
	
	public function requestActivityAction() {
		if (isset ( $_POST ["activityType"] )) {
			if (isset ( $_POST ["id"] )) {
				$this->updateActivity ();
			} else {
				if ($this->createNewActivity ()) {
					echo getAjaxResult ( true, "成功", "请在分类列表中查看新增条目！" );
				} else {
					echo getAjaxResult ( false, "失败", "数据未被保存！", "forbid" );
				}
			}
			$_GET ['action'] = "viewapprovingactivities";
			$this->viewApprovingActivitiesAction();
		
		} else {
			$this->view->render ( "activity_request.php" );
		}
	}
	
	public function viewSpecificActivityAction() {
		if (isset ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			setRollback ( "查看", 1, array ("id" => $id ) );
			
			$sql = "select * from `coursesystem`.`activity` where id='$id'";
			$db = MysqliObj::getInstance ();
			$dbData = $db->fetch_array ( $sql );
			$data = new ActivityBean ( $dbData );
		}
		
		$this->view->render ( "activity_detail.php", $data, true );
	}
	
	public function editSpecificActivityAction() {
		if (isset ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			setRollback ( "修改", 1, array ("id" => $id ) );
			
			$sql = "select * from activity where id='$id'";
			$db = MysqliObj::getInstance ();
			$dbData = $db->fetch_array ( $sql );
			$data = new ActivityBean ( $dbData );
			if (! ($data->department == $_SESSION ['userinfo'] ['department'] || isAdmin ())) {
				$data = new CourseBean ();
			}
		}
		$this->view->render ( "activity_edit.php", $data, true );
	}
	
	function updateAction() {
		if (isset ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$activity = ActivityBean::findById ( $id );
			if ($activity == null) {
				echo getAjaxResult ( false, "提示", "该条信息不存在或者不可被修改，请刷新后再试！" );
			} elseif ($activity->approval == 1) {
				echo getAjaxResult ( false, "提示", "该条信息以通过审核，请刷新后再试！" );
			} elseif ($activity->department != $_SESSION ['userinfo'] ['department'] || isAdmin ()) {
				echo getAjaxResult ( false, "提示", "您无法进行该操作！" );
			} else {
			}
			$this->updateActivity ();
		}
		$_GET ['action'] = "viewapprovingactivities";
		$this->viewApprovingCoursesAction ();
	
	}
	
	public function deleteAction() {
		if (isset ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$sql = "select * from activity where id='$id'";
			$db = MysqliObj::getInstance ();
			$dbData = $db->fetch_array ( $sql );
			$data = new ActivityBean ( $dbData );
			if (($data->department == $_SESSION ['userinfo'] ['department'] || isAdmin ())) {
				$sql = "delete from activity where id='$id'";
				$result = $db->query ( $sql );
				if ($result) {
					echo getAjaxResult ( true, "提示", "删除成功！" );
				}
			} else {
				echo getAjaxResult ( false, "错误", "未能成功删除！" );
			}
		
		}
		$this->viewApprovingActivitiesAction ();
	}
	
	public function viewApprovingActivitiesAction() {
		if (isset ( $_SESSION ['userinfo'] )) {
			$info = $_SESSION ['userinfo'];
			$type = $info ['type'];
			
			if (isset ( $_GET ['page'] )) {
				$page = $_GET ['page'];
			} else {
				$page = 1;
			}
			
			$pageSize = Sysconfig::getPageSize ();
			$begin = ($page - 1) * $pageSize;
			
			if (UserBean::$_ADMIN == $type || UserBean::$_SENATE == $type) {
				$sql = "select * from `coursesystem`.`activity` where approval='0' limit $begin, $pageSize";
				$sqlForNum = "select * from `coursesystem`.`activity` where approval='0'";
			} else if (UserBean::$_INSTITUE == $type) {
				$department = $info ['department'];
				$sql = "select * from `coursesystem`.`activity` where department='$department' and approval='0' limit $begin, $pageSize";
				$sqlForNum = "select * from `coursesystem`.`activity` where department='$department' and approval='0'";
			}
			
			$db = MysqliObj::getInstance ();
			$resultArray = $db->fetch_arrays ( $sql );
			
			$activities = array ();
			if (! empty ( $resultArray )) {
				foreach ( $resultArray as $item ) {
					$activities [] = new ActivityBean ( $item );
				}
			}
			
			$data = array ();
			$data ['currentPage'] = $page;
			$data ['activities'] = $activities;
			
			$result = $db->query ( $sqlForNum );
			if (! empty ( $result )) {
				$num = ( int ) ($result->num_rows / $pageSize);
				$data ['pageNum'] = $result->num_rows % $pageSize == 0 ? $num : $num + 1;
			} else {
				$data ['pageNum'] = 0;
			}
		}
		$data ['title'] = "正在处理";
		setRollback ( "正在处理", 0, (isset ( $data ['currentPage'] )) ? array ("page" => $data ['currentPage'] ) : array () );
		$this->view->render ( "activity_list.php", $data );
	}
	
	public function viewApprovedActivitiesAction() {
		if (isset ( $_SESSION ['userinfo'] )) {
			$info = $_SESSION ['userinfo'];
			$type = $info ['type'];
			
			if (isset ( $_GET ['page'] )) {
				$page = $_GET ['page'];
			} else {
				$page = 1;
			}
			
			$pageSize = Sysconfig::getPageSize ();
			$begin = ($page - 1) * $pageSize;
			
			if (UserBean::$_ADMIN == $type || UserBean::$_SENATE == $type) {
				$sql = "select * from `coursesystem`.`activity` where approval='1' limit $begin, $pageSize";
				$sqlForNum = "select * from `coursesystem`.`activity` where approval='1'";
			} else if (UserBean::$_INSTITUE == $type) {
				$department = $info ['department'];
				$sql = "select * from `coursesystem`.`activity` where department='$department' and approval='1' limit $begin, $pageSize";
				$sqlForNum = "select * from `coursesystem`.`activity` where department='$department' and approval='1'";
			}
			
			$db = MysqliObj::getInstance ();
			$resultArray = $db->fetch_arrays ( $sql );
			
			$activities = array ();
			if (! empty ( $resultArray )) {
				foreach ( $resultArray as $item ) {
					$activities [] = new ActivityBean ( $item );
				}
			}
			
			$data = array ();
			$data ['currentPage'] = $page;
			$data ['activities'] = $activities;
			
			$result = $db->query ( $sqlForNum );
			if (! empty ( $result )) {
				$num = ( int ) ($result->num_rows / $pageSize);
				$data ['pageNum'] = $result->num_rows % $pageSize == 0 ? $num : $num + 1;
			} else {
				$data ['pageNum'] = 0;
			}
		}
		$data ['title'] = "已处理";
		setRollback ( "已处理", 0, (isset ( $data ['currentPage'] )) ? array ("page" => $data ['currentPage'] ) : array () );
		$this->view->render ( "activity_list.php", $data );
	}
	
	public function viewAllActivitiesAction() {
		if (isset ( $_SESSION ['userinfo'] )) {
			$info = $_SESSION ['userinfo'];
			$type = $info ['type'];
			
			if (isset ( $_GET ['page'] )) {
				$page = $_GET ['page'];
			} else {
				$page = 1;
			}
			
			$pageSize = Sysconfig::getPageSize ();
			$begin = ($page - 1) * $pageSize;
			if (UserBean::$_ADMIN == $type || UserBean::$_SENATE == $type) {
				$sql = "select * from `coursesystem`.`activity` limit $begin, $pageSize";
				$sqlForNum = "select * from `coursesystem`.`activity`";
			} else if (UserBean::$_INSTITUE == $type) {
				$department = $info ['department'];
				$sql = "select * from `coursesystem`.`activity` where department='$department' limit $begin, $pageSize";
				$sqlForNum = "select * from `coursesystem`.`activity` where department='$department'";
			}
			
			$db = MysqliObj::getInstance ();
			$resultArray = $db->fetch_arrays ( $sql );
			
			$activities = array ();
			if (! empty ( $resultArray )) {
				foreach ( $resultArray as $item ) {
					$activities [] = new ActivityBean ( $item );
				}
			}
			
			$data = array ();
			$data ['currentPage'] = $page;
			$data ['activities'] = $activities;
			
			$result = $db->query ( $sqlForNum );
			if (! empty ( $result )) {
				$num = ( int ) ($result->num_rows / $pageSize);
				$data ['pageNum'] = $result->num_rows % $pageSize == 0 ? $num : $num + 1;
			} else {
				$data ['pageNum'] = 0;
			}
		}
		$data ['title'] = "全部申请";
		setRollback ( "全部申请", 0, (isset ( $data ['currentPage'] )) ? array ("page" => $data ['currentPage'] ) : array () );
		$this->view->render ( "activity_list.php", $data );
	}
	
	public function approveActivityAction() {
		if (isset ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$db = MysqliObj::getInstance ();
			$sql = "UPDATE `coursesystem`.`activity` SET `approval` = '1' WHERE `activity`.`id` = $id;";
			$result = $db->query ( $sql );
			if ($result) {
				$data = true;
				echo getAjaxResult ( true, "提示", "该条目成功通过审批！" );
			}
		} else {
			$data = false;
			echo getAjaxResult ( false, "失败", "该条目可能不存在或者已通过审批！" );
		}
		
		$this->viewApprovedActivitiesAction();
	}
	
	public function printAction() {
		if (isset ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$sql = "select * from activity where id='$id'";
			$db = MysqliObj::getInstance ();
			$data = $db->fetch_array ( $sql );
			if ($data) {
				$data = new ActivityBean ( $data );
				$this->view->render ( "activity_print.php", $data );
			}
		}
	}
	
	public function to404Action() {
		$this->view->render ( "404.php" );
	}
	
	//当请求的	action 不合法时调用此方法
	//重载 IController 接口
	public function forbidAction() {
		if ($_SESSION ['userinfo'] ['type'] == UserBean::$_GUEST) {
			redirectTo ( urlToAction ( 'user', 'login' ) );
		}
		$this->view->render ( "forbid.php" );
	}
	
	private function createNewActivity() {
		$activityType = $_POST ["activityType"];
		$type = $_POST ["type"];
		$department = $_POST ["department"];
		$name = $_POST ["name"];
		$host = $_POST ["host"];
		$activityBegin = $_POST ["activityBegin"];
		$activityEnd = $_POST ["activityEnd"];
		$location = $_POST ["location"];
		$content = $_POST ["content"];
		$budget = $_POST ["budget"];
		$time = $_POST ["time"];
		//$applicant = $_POST ["applicant"];
		$new = 1;
		$approval = 0;
		
		$dbms = MysqliObj::getInstance ();
		$sql = "INSERT INTO activity(activityType, type, department, name, host, activityBegin, activityEnd, location, content, budget, time, new, approval) VALUES('$activityType', '$type', '$department', '$name', '$host', '$activityBegin', '$activityEnd', '$location', '$content', '$budget', '$time', '$new', '$approval')";
		$dbms->real_escape_string ( $sql );
		$result = $dbms->query ( $sql );
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
	
	private function updateActivity() {
		$id = $_POST ["id"];
		$activityType = $_POST ["activityType"];
		$type = $_POST ["type"];
		$department = $_POST ["department"];
		$name = $_POST ["name"];
		$host = $_POST ["host"];
		$activityBegin = $_POST ["activityBegin"];
		$activityEnd = $_POST ["activityEnd"];
		$location = $_POST ["location"];
		$content = $_POST ["content"];
		$budget = $_POST ["budget"];
		$time = $_POST ["time"];
		$new = 1;
		$approval = 0;
		$applicant = 0;
		
		// 		$course = new CourseBean();
		// 		$course->setCourseName($courseName);
		// 		$course->setDepartment($department);
		// 		$course->setTeacherType($teacherType);
		// 		$course->setTeacherName($teacherName);
		// 		$course->setOrganization($organization);
		// 		$course->setJobTitle($jobTitle);
		// 		$course->setCredential($credential);
		// 		$course->setCourseBegin($courseBegin);
		// 		$course->setCourseEnd($courseEnd);
		// 		$course->setSalary($salary);
		// 		$course->setSalaryStandard($salaryStandard);
		// 		$course->setTime($time);
		// 		$course->setUnit($unit);
		// 		$course->setCourseType($courseType);
		// 		$course->setNew($new);
		

		$dbms = MysqliObj::getInstance ();
		
		$sql = "UPDATE `coursesystem`.`activity` SET `activityType`='$activityType', `type` = '$type', `department`='$department', `name`='$name', `host`='$host', `activityBegin`='$activityBegin',
		`activityEnd`='$activityEnd', `location`='$location', `content`='$content', `budget`='$budget', `time`='$time',`new`='$new' ,`approval`='$approval'
		WHERE `activity`.`id` = '$id' ";
		$dbms->real_escape_string ( $sql );
		$result = $dbms->query ( $sql );
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
	
// 	private function assemble() {
// 		$activity = new ActivityBean ();
// 		if (! empty ( $_POST )) {
// 			foreach ( $_POST as $key => $val ) {
// 				$activity->$key = $val;
// 			}
// 		}
// 		return $activity;
// 	}
}

?>